/**
 * @file setting_mobile_network.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_MOBILE_NETWORK != 0

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static lv_mobile_network_obj_ext_t * mobile_network_get_ext(void);
static void mobile_network_get_apn_name(char *apn);
static uint8_t mobile_network_get_ip_type(void);
static void mobile_network_switch_action(lv_obj_t * obj, lv_event_t e);
static void mobile_network_data_roaming_switch_action(lv_obj_t * obj, lv_event_t e);
static void mobile_network_modify_apn_action(lv_obj_t * btn, lv_event_t e);
static void mobile_network_reset_apn_action(lv_obj_t * btn, lv_event_t e);
static void mobile_network_prepare_destory(lv_obj_t * activity_obj);
static lv_obj_t * mobile_network_create(lv_obj_t * activity_obj);

static lv_apn_obj_ext_t * apn_get_ext(void);
#if USE_LV_WATCH_KEYPAD != 0
static void apn_get_keypad_text(char *text);
#if USE_LV_WATCH_CCFC != 0
static void divert_num_get_keypad_text(char *text);
#endif
#endif
static void apn_confirm_btn_action(lv_obj_t * btn, lv_event_t e);
static void apn_cancel_btn_action(lv_obj_t * btn, lv_event_t e);
static void apn_edit_btn_action(lv_obj_t * btn, lv_event_t e);
static void apn_prepare_destory(lv_obj_t * activity_obj);
static lv_obj_t * apn_create(lv_obj_t * activity_obj);
#if USE_LV_WATCH_CCWA != 0
static void mobile_network_call_wait_switch_action(lv_obj_t * obj, lv_event_t e);
#endif
#if USE_LV_WATCH_CCFC != 0
static void call_forward_prepare_destory(lv_obj_t * activity_obj);
static lv_obj_t * call_forward_create(lv_obj_t * activity_obj);
static void divert_num_edit_btn_action(lv_obj_t * btn, lv_event_t e);
static void divert_confirm_btn_action(lv_obj_t * btn, lv_event_t e);
static void divert_cancel_btn_action(lv_obj_t * btn, lv_event_t e);
static void call_forward_common_prepare_destory(lv_obj_t * activity_obj);
static lv_obj_t * call_forward_common_create(lv_obj_t * activity_obj);
static lv_call_forward_obj_ext_t * call_forward_get_ext(void);
static void mobile_network_no_condition_action(lv_obj_t * btn, lv_event_t e);
static void mobile_network_busy_action(lv_obj_t * btn, lv_event_t e);
static void mobile_network_not_reach_action(lv_obj_t * btn, lv_event_t e);
static void mobile_network_no_answer_action(lv_obj_t * btn, lv_event_t e);
static const char g_accept_input_num[13] = { '+', '1', '2', '3', '4', '5', '6', '7', '8', '9', '*', '#', '0' };
#endif
/**********************
 *  STATIC VARIABLES
 **********************/
static nv_apn_info apn_info[2];

LV_IMG_DECLARE(icon_cancel_big);
LV_IMG_DECLARE(icon_confirm_big);

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
void mobile_network_create_btn_action(lv_obj_t * btn, lv_event_t event)
{
    (void)btn;
    if(LV_EVENT_CLICKED != event) return;

    lv_watch_png_cache_all_free();

    lv_obj_t * obj = mobile_network_create(NULL);
    LV_ASSERT_MEM(obj);
}

void mobile_network_init(nv_apn_info *nv_apn)
{
    Hal_Mem_Copy(apn_info, nv_apn, sizeof(nv_apn_info) * 2);
}

MMI_Modem_Apn_Info_t * mobile_network_get_apn_info(void)
{
    return (MMI_Modem_Apn_Info_t *)apn_info;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static lv_mobile_network_obj_ext_t * mobile_network_get_ext(void)
{
    lv_mobile_network_obj_ext_t  * ext = NULL;
    lv_obj_t * watch_obj = NULL;

    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_MOBILE_NETWORK);
    if(activity_obj) {
        lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    }
    if(watch_obj) {
        ext = lv_obj_get_ext_attr(watch_obj);
    }
    return ext;
}

static void mobile_network_get_apn_name(char *apn)
{
    MMI_MODEM_SIM_ID sim_id = watch_modem_get_sim_id();

    if(0 == apn_info[sim_id].apnflg) {
        watch_modem_get_default_apn_name(apn, NV_MAX_APN_INFO_LEN);
    } else {
        strncpy(apn, apn_info[sim_id].apn, NV_MAX_APN_INFO_LEN);
    }
}

static uint8_t mobile_network_get_ip_type(void)
{
    uint8_t ip_type;
    MMI_MODEM_SIM_ID sim_id = watch_modem_get_sim_id();

    if(0 == apn_info[sim_id].apnflg) {
        ip_type = watch_modem_get_default_ip_type();
    } else {
        ip_type = apn_info[sim_id].iptype;
    }

    return ip_type;
}

static void mobile_network_switch_action(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_VALUE_CHANGED != e) {
        return;
    }

    bool sw_state = lv_switch_get_state(obj);
    bool on_off = sw_state ? true: false;

    printf("%s: %d\n", __FUNCTION__, on_off);
#if USE_LV_WLAN != 0
    setting_set_mobile_data_switch(on_off);
#else
    watch_modem_set_connection_switch(on_off);
#endif

    //config manual apn
    if(on_off) {
        MMI_MODEM_SIM_ID sim_id = watch_modem_get_sim_id();
        if(apn_info[sim_id].apnflg) {
            watch_modem_config_apn(apn_info[sim_id].apn, apn_info[sim_id].iptype);
        }
    }
}

static void mobile_network_data_roaming_switch_action(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_VALUE_CHANGED != e) {
        return;
    }

    bool sw_state = lv_switch_get_state(obj);
    BOOL on_off = sw_state ? 1: 0;

    printf("%s: %d\n", __FUNCTION__, on_off);
    watch_modem_set_data_roaming(on_off);
}
#if USE_LV_WATCH_CCWA != 0
static void mobile_network_call_wait_switch_action(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_VALUE_CHANGED != e) {
        return;
    }

    bool sw_state = lv_switch_get_state(obj);
    BOOL on_off = sw_state ? 1: 0;

    printf("%s: %d\n", __FUNCTION__, on_off);
    watch_modem_call_wait_set_req(on_off);
}
#endif
#if USE_LV_WATCH_CCFC != 0
static void mobile_network_modify_call_forward_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    call_forward_create(NULL);
}

static void mobile_network_no_condition_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }
    lv_call_forward_obj_ext_t * ext = call_forward_get_ext();
    LV_ASSERT_MEM(ext);
    ext->divert_page = WATCH_CALL_FORWARD_NO_CONDITION;
    call_forward_common_create(NULL);
}

static void mobile_network_busy_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }
    lv_call_forward_obj_ext_t * ext = call_forward_get_ext();
    LV_ASSERT_MEM(ext);
    ext->divert_page = WATCH_CALL_FORWARD_BUSY;
    call_forward_common_create(NULL);
}

static void mobile_network_not_reach_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }
    lv_call_forward_obj_ext_t * ext = call_forward_get_ext();
    LV_ASSERT_MEM(ext);
    ext->divert_page = WATCH_CALL_FORWARD_NOT_REACH;
    call_forward_common_create(NULL);
}

static void mobile_network_no_answer_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }
    lv_call_forward_obj_ext_t * ext = call_forward_get_ext();
    LV_ASSERT_MEM(ext);
    ext->divert_page = WATCH_CALL_FORWARD_NO_ANSWER;
    call_forward_common_create(NULL);
}
#endif
static void mobile_network_modify_apn_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    apn_create(NULL);
}

static void mobile_network_reset_apn_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    lv_mobile_network_obj_ext_t * ext = mobile_network_get_ext();
    if(NULL == ext) {
        return;
    }

    MMI_MODEM_SIM_ID sim_id = watch_modem_get_sim_id();
    if(0 == apn_info[sim_id].apnflg) {
        printf("%s: is already default apn, no need to reset\n", __FUNCTION__);
        return;
    }

    //write to NVM
    memset(&apn_info[sim_id], 0, sizeof(nv_apn_info));
    UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, NV_APN_OFFSET, sizeof(nv_apn_info) * 2, (uint8_t *)apn_info);

    //set apn info
    watch_modem_set_apn_info((MMI_Modem_Apn_Info_t *)apn_info);

    //reset apn
    watch_modem_reset_apn();

    //reset apn_name & ip_type
    watch_modem_get_default_apn_name(ext->apn_name, NV_MAX_APN_INFO_LEN);
    ext->ip_type = watch_modem_get_default_ip_type();
    lv_label_set_text(ext->label_apn_name, ext->apn_name);
    printf("%s: %s, %d\n", __FUNCTION__, ext->apn_name, ext->ip_type);
}

static void mobile_network_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
}

static lv_obj_t * mobile_network_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_MOBILE_NETWORK;
        activity_ext.create = mobile_network_create;
        activity_ext.prepare_destory = mobile_network_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) return NULL;
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return NULL;
    lv_mobile_network_obj_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_mobile_network_obj_ext_t));
    LV_ASSERT_MEM(ext);
    mobile_network_get_apn_name(ext->apn_name);
    ext->ip_type = mobile_network_get_ip_type();
    printf("%s: %s, %d\n", __FUNCTION__, ext->apn_name, ext->ip_type);

    lv_obj_t * title_label = lv_label_create(obj, NULL);
    lv_obj_add_style(title_label, LV_CONT_PART_MAIN, &lv_watch_font20_gray);
    lv_label_set_align(title_label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(title_label, false);
    lv_label_set_text_id(title_label, WATCH_TEXT_ID_MOBILE_NETWORK);
    lv_obj_align(title_label, obj, LV_ALIGN_IN_TOP_MID, 0, 10);

    lv_obj_t * page = lv_page_create(obj, NULL);
    lv_obj_set_size(page, LV_HOR_RES, LV_VER_RES - lv_obj_get_height(title_label) - 26);
    lv_obj_align(page, title_label, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_obj_add_style(page, LV_PAGE_PART_BG, &lv_style_transp);
    lv_obj_add_style(page, LV_PAGE_PART_SCROLLABLE, &lv_style_transp);
    lv_obj_add_style(page, LV_PAGE_PART_EDGE_FLASH, &lv_watch_cont_opa1);
    lv_page_set_scrollbar_mode(page, LV_SCROLLBAR_MODE_OFF);
    lv_watch_obj_add_element(page);
    lv_watch_obj_add_element(lv_page_get_scrl(page));

    //mobile data
    lv_obj_t * content = lv_cont_create(page, NULL);
    lv_obj_set_size(content, LV_HOR_RES, 40);
    lv_obj_add_style(content, LV_CONT_PART_MAIN, &lv_watch_style_transp);
    lv_obj_set_click(content, false);
    lv_obj_align(content, title_label, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);

    lv_obj_t * sw_label = lv_label_create(content, NULL);
    lv_label_set_text_id(sw_label, WATCH_TEXT_ID_MOBILE_DATA);
    lv_obj_add_style(sw_label, LV_CONT_PART_MAIN, &lv_watch_font20);

    lv_obj_t * sw = lv_switch_create(content, NULL);
    lv_obj_set_size(sw, LV_HOR_RES / 4, 30);
    lv_obj_set_event_cb(sw, mobile_network_switch_action);
    lv_obj_set_style_local_bg_color(sw, LV_SWITCH_PART_BG, LV_STATE_DEFAULT, LV_COLOR_SILVER);
    lv_obj_set_style_local_bg_color(sw, LV_SWITCH_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_SILVER);
    lv_obj_set_style_local_bg_color(sw, LV_SWITCH_PART_BG, LV_STATE_CHECKED, LV_COLOR_MAKE(0x15, 0x68, 0xD2));
    lv_obj_set_style_local_bg_color(sw, LV_SWITCH_PART_INDIC, LV_STATE_CHECKED, LV_COLOR_MAKE(0x15, 0x68, 0xD2));
    lv_watch_obj_add_element(sw);

#if USE_LV_WLAN != 0
    if(setting_get_mobile_data_switch()) {
        lv_switch_on(sw, LV_ANIM_OFF);
    } else {
        lv_switch_off(sw, LV_ANIM_OFF);
    }
#else
    if(watch_modem_get_connection_switch()) {
        lv_switch_on(sw, LV_ANIM_OFF);
    } else {
        lv_switch_off(sw, LV_ANIM_OFF);
    }
#endif

    //data roaming
    lv_obj_t * cont_roaming = lv_cont_create(page, content);
    lv_obj_align(cont_roaming, content, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);

    lv_obj_t * sw_roaming_label = lv_label_create(cont_roaming, sw_label);
    lv_label_set_text_id(sw_roaming_label, WATCH_TEXT_ID_DATA_ROAMING);

    lv_obj_t * sw_roaming = lv_switch_create(cont_roaming, NULL);
    lv_obj_set_size(sw_roaming, LV_HOR_RES / 4, 30);
    lv_obj_set_event_cb(sw_roaming, mobile_network_data_roaming_switch_action);
    lv_obj_set_style_local_bg_color(sw_roaming, LV_SWITCH_PART_BG, LV_STATE_DEFAULT, LV_COLOR_SILVER);
    lv_obj_set_style_local_bg_color(sw_roaming, LV_SWITCH_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_SILVER);
    lv_obj_set_style_local_bg_color(sw_roaming, LV_SWITCH_PART_BG, LV_STATE_CHECKED, LV_COLOR_MAKE(0x15, 0x68, 0xD2));
    lv_obj_set_style_local_bg_color(sw_roaming, LV_SWITCH_PART_INDIC, LV_STATE_CHECKED, LV_COLOR_MAKE(0x15, 0x68, 0xD2));
    lv_watch_obj_add_element(sw_roaming);

    BOOL roaming_on_off = watch_modem_get_data_roaming();
    printf("%s: roaming %d\n", __FUNCTION__, roaming_on_off);
    if(roaming_on_off) {
        lv_switch_on(sw_roaming, LV_ANIM_OFF);
    } else {
        lv_switch_off(sw_roaming, LV_ANIM_OFF);
    }

#if USE_LV_WATCH_CCWA != 0
    //call wait
    lv_obj_t * cont_call_wait = lv_cont_create(page, content);
    lv_obj_align(cont_call_wait, cont_roaming, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);

    lv_obj_t * call_wait_label = lv_label_create(cont_call_wait, sw_label);
    lv_label_set_text_id(call_wait_label, WATCH_TEXT_ID_CALL_WAIT);

    lv_obj_t * sw_wait = lv_switch_create(cont_call_wait, NULL);
    lv_obj_set_size(sw_wait, LV_HOR_RES / 4, 30);
    lv_obj_set_event_cb(sw_wait, mobile_network_call_wait_switch_action);
    lv_obj_set_style_local_bg_color(sw_wait, LV_SWITCH_PART_BG, LV_STATE_DEFAULT, LV_COLOR_SILVER);
    lv_obj_set_style_local_bg_color(sw_wait, LV_SWITCH_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_SILVER);
    lv_obj_set_style_local_bg_color(sw_wait, LV_SWITCH_PART_BG, LV_STATE_CHECKED, LV_COLOR_MAKE(0x15, 0x68, 0xD2));
    lv_obj_set_style_local_bg_color(sw_wait, LV_SWITCH_PART_INDIC, LV_STATE_CHECKED, LV_COLOR_MAKE(0x15, 0x68, 0xD2));
    lv_watch_obj_add_element(sw_wait);

    BOOL call_wait_on_off = watch_modem_call_wait_query_req();
    printf("%s: call wait %d\n", __FUNCTION__, call_wait_on_off);
    if(call_wait_on_off) {
        lv_switch_on(sw_wait, LV_ANIM_OFF);
    } else {
        lv_switch_off(sw_wait, LV_ANIM_OFF);
    }
#endif
#if USE_LV_WATCH_CCFC != 0
    //call forward
#if USE_LV_WATCH_CCWA != 0
    lv_obj_t * cont_call_forward = lv_cont_create(page, cont_call_wait);
    lv_obj_align(cont_call_forward, cont_call_wait, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
#else
    lv_obj_t * cont_call_forward = lv_cont_create(page, content);
    lv_obj_align(cont_call_forward, cont_roaming, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
#endif
    lv_obj_set_click(cont_call_forward, true);
    lv_obj_set_event_cb(cont_call_forward, mobile_network_modify_call_forward_action);
    lv_page_glue_obj(cont_call_forward, true);
    lv_watch_obj_add_element(cont_call_forward);

    lv_obj_t * call_forward_label = lv_label_create(cont_call_forward, sw_label);
    lv_label_set_text_id(call_forward_label, WATCH_TEXT_ID_CALL_FORWARD);

    lv_obj_t * forward_btn_right = lv_imgbtn_create(cont_call_forward, NULL);
    lv_obj_set_style_local_text_font(forward_btn_right, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_SYMBOL_NORMAL);
    lv_obj_set_click(forward_btn_right, false);
#endif
    //APN
    lv_obj_t * cont_apn = lv_cont_create(page, NULL);
    lv_obj_set_size(cont_apn, LV_HOR_RES, 60);
    lv_obj_add_style(cont_apn, LV_CONT_PART_MAIN, &lv_watch_style_transp);
    lv_obj_set_click(cont_apn, true);
    lv_watch_obj_add_element(cont_apn);
    lv_page_glue_obj(cont_apn, true);
#if USE_LV_WATCH_CCFC != 0
    lv_obj_align(cont_apn, cont_call_forward, LV_ALIGN_OUT_BOTTOM_MID, 0, 16);
#else
#if USE_LV_WATCH_CCWA != 0
    lv_obj_align(cont_apn, cont_call_wait, LV_ALIGN_OUT_BOTTOM_MID, 0, 16);
#else
    lv_obj_align(cont_apn, cont_roaming, LV_ALIGN_OUT_BOTTOM_MID, 0, 16);
#endif
#endif

    lv_obj_t * label_apn = lv_label_create(cont_apn, NULL);
    char str[40];
    memset(str, 0, 40);
    if(MMI_MODEM_SIM_2 == watch_modem_get_sim_id()) {
        snprintf(str, 40, "%s %s", (char *)lv_lang_get_text(WATCH_TEXT_ID_SIM2), (char *)lv_lang_get_text(WATCH_TEXT_ID_APN));
    } else {
        snprintf(str, 40, "%s %s", (char *)lv_lang_get_text(WATCH_TEXT_ID_SIM1), (char *)lv_lang_get_text(WATCH_TEXT_ID_APN));
    }
    lv_label_set_text(label_apn, str);
    lv_obj_add_style(label_apn, LV_CONT_PART_MAIN, &lv_watch_font20);

    //APN name
    lv_obj_t * label_apn_name = lv_label_create(cont_apn, NULL);
    if(strlen(ext->apn_name) > 0) {
        lv_label_set_text(label_apn_name, ext->apn_name);
    } else {
        lv_label_set_text(label_apn_name, "");
    }
    lv_obj_add_style(label_apn_name, LV_CONT_PART_MAIN, &lv_watch_font20);
    ext->label_apn_name = label_apn_name;

    //modify APN
    lv_obj_t * btn_right = lv_imgbtn_create(cont_apn, NULL);
    lv_obj_set_style_local_text_font(btn_right, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_SYMBOL_NORMAL);
    lv_obj_set_event_cb(cont_apn, mobile_network_modify_apn_action);
    lv_obj_set_click(btn_right, false);

    //reset APN
    lv_obj_t * btn_reset = lv_btn_create(page, NULL);
    lv_obj_set_size(btn_reset, 160, 40);
    lv_obj_align(btn_reset, cont_apn, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_obj_set_event_cb(btn_reset, mobile_network_reset_apn_action);
    lv_obj_t * label_reset = lv_label_create(btn_reset, NULL);
    lv_label_set_text_id(label_reset, WATCH_TEXT_ID_RESET_APN);
    lv_obj_add_style(label_reset, LV_CONT_PART_MAIN, &lv_watch_font20);
    lv_watch_obj_add_element(btn_reset);

    if(LV_BIDI_DIR_RTL == lv_obj_get_base_dir(page)) {
        lv_obj_align(sw_label, NULL, LV_ALIGN_IN_RIGHT_MID, -10, 0);
        lv_obj_align(sw, NULL, LV_ALIGN_IN_LEFT_MID, 10, 0);
        lv_obj_align(sw_roaming_label, NULL, LV_ALIGN_IN_RIGHT_MID, -10, 0);
        lv_obj_align(sw_roaming, NULL, LV_ALIGN_IN_LEFT_MID, 10, 0);
#if USE_LV_WATCH_CCWA != 0
        lv_obj_align(call_wait_label, NULL, LV_ALIGN_IN_RIGHT_MID, -10, 0);
        lv_obj_align(sw_wait, NULL, LV_ALIGN_IN_LEFT_MID, 10, 0);
#endif
#if USE_LV_WATCH_CCFC != 0
        lv_obj_align(call_forward_label, NULL, LV_ALIGN_IN_RIGHT_MID, -10, 0);
        lv_imgbtn_set_src(forward_btn_right, LV_BTN_STATE_RELEASED, LV_SYMBOL_LEFT);
        lv_imgbtn_set_src(forward_btn_right, LV_BTN_STATE_PRESSED, LV_SYMBOL_LEFT);
        lv_obj_align(forward_btn_right, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 10, 0);
#endif
        lv_obj_align(label_apn, NULL, LV_ALIGN_IN_TOP_RIGHT, -10, 0);
        lv_obj_align(label_apn_name, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -10, 0);
        lv_imgbtn_set_src(btn_right, LV_BTN_STATE_RELEASED, LV_SYMBOL_LEFT);
        lv_imgbtn_set_src(btn_right, LV_BTN_STATE_PRESSED, LV_SYMBOL_LEFT);
        lv_obj_align(btn_right, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 10, 0);
    } else {
        lv_obj_align(sw_label, NULL, LV_ALIGN_IN_LEFT_MID, 10, 0);
        lv_obj_align(sw, NULL, LV_ALIGN_IN_RIGHT_MID, -10, 0);
        lv_obj_align(sw_roaming_label, NULL, LV_ALIGN_IN_LEFT_MID, 10, 0);
        lv_obj_align(sw_roaming, NULL, LV_ALIGN_IN_RIGHT_MID, -10, 0);
#if USE_LV_WATCH_CCWA != 0
        lv_obj_align(call_wait_label, NULL, LV_ALIGN_IN_LEFT_MID, 10, 0);
        lv_obj_align(sw_wait, NULL, LV_ALIGN_IN_RIGHT_MID, -10, 0);
#endif
#if USE_LV_WATCH_CCFC != 0
        lv_obj_align(call_forward_label, NULL, LV_ALIGN_IN_LEFT_MID, 10, 0);
        lv_imgbtn_set_src(forward_btn_right, LV_BTN_STATE_RELEASED, LV_SYMBOL_RIGHT);
        lv_imgbtn_set_src(forward_btn_right, LV_BTN_STATE_PRESSED, LV_SYMBOL_RIGHT);
        lv_obj_align(forward_btn_right, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -10, 0);
#endif
        lv_obj_align(label_apn, NULL, LV_ALIGN_IN_TOP_LEFT, 10, 0);
        lv_obj_align(label_apn_name, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 10, 0);
        lv_imgbtn_set_src(btn_right, LV_BTN_STATE_RELEASED, LV_SYMBOL_RIGHT);
        lv_imgbtn_set_src(btn_right, LV_BTN_STATE_PRESSED, LV_SYMBOL_RIGHT);
        lv_obj_align(btn_right, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -10, 0);
    }

    return obj;
}

static lv_apn_obj_ext_t * apn_get_ext(void)
{
    lv_apn_obj_ext_t  * ext = NULL;
    lv_obj_t * watch_obj = NULL;

    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_APN);
    if(activity_obj) {
        lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    }
    if(watch_obj) {
        ext = lv_obj_get_ext_attr(watch_obj);
    }
    return ext;
}
#if USE_LV_WATCH_CCFC != 0
static lv_call_forward_obj_ext_t * call_forward_get_ext(void)
{
    lv_call_forward_obj_ext_t  * ext = NULL;
    lv_obj_t * watch_obj = NULL;

    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_CALL_FORWARD);
    if(activity_obj) {
        lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    }
    if(watch_obj) {
        ext = lv_obj_get_ext_attr(watch_obj);
    }
    return ext;
}
#endif
#if USE_LV_WATCH_KEYPAD != 0
static void apn_get_keypad_text(char *text)
{
    lv_apn_obj_ext_t * ext = apn_get_ext();
    if(NULL == ext) return;

    if(text) {
        lv_textarea_set_text(ext->ta_name, text);
    }
}
#endif

static void apn_confirm_btn_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    lv_mobile_network_obj_ext_t * network_ext = mobile_network_get_ext();
    lv_apn_obj_ext_t * apn_ext = apn_get_ext();
    if((NULL == network_ext) || (NULL == apn_ext)) {
        return;
    }

    const char * name = lv_textarea_get_text(apn_ext->ta_name);
    if(0 == strlen(name)) {
        printf("%s: strlen(name) == 0\n", __FUNCTION__);
        return;
    }

    uint8_t ip_type = CM_IPTYPE_IP;
    uint8_t index = lv_dropdown_get_selected(apn_ext->ddlist_ip_type);
    switch(index) {
        case 0:
            ip_type= CM_IPTYPE_IP;
            break;

        case 1:
            ip_type= CM_IPTYPE_IPV6;
            break;

        case 2:
            ip_type= CM_IPTYPE_IPV4V6;
            break;

        default:
            break;
    }

    printf("%s: old(%s, %d), new(%s, %d)\n", __FUNCTION__, network_ext->apn_name, network_ext->ip_type, name, ip_type);

    if(strcmp(name, network_ext->apn_name) || (ip_type != network_ext->ip_type)) {
        //update apn_name & ip_type
        network_ext->ip_type = ip_type;
        strncpy(network_ext->apn_name, name, NV_MAX_APN_INFO_LEN);
        lv_label_set_text(network_ext->label_apn_name, network_ext->apn_name);

        //write to NVM
        MMI_MODEM_SIM_ID sim_id = watch_modem_get_sim_id();
        apn_info[sim_id].apnflg = 1;
        apn_info[sim_id].iptype = ip_type;
        strncpy(apn_info[sim_id].apn, name, NV_MAX_APN_INFO_LEN);
        UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, NV_APN_OFFSET, sizeof(nv_apn_info) * 2, (uint8_t *)apn_info);

        //set apn info
        watch_modem_set_apn_info((MMI_Modem_Apn_Info_t *)apn_info);

        //config apn
        watch_modem_config_apn(network_ext->apn_name, network_ext->ip_type);
    }

    lv_watch_obj_cleanup(btn);
}

static void apn_cancel_btn_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    lv_watch_obj_cleanup(btn);
}

static void apn_edit_btn_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    lv_apn_obj_ext_t * apn_ext = apn_get_ext();
    if(NULL == apn_ext) {
        return;
    }

#if USE_LV_WATCH_KEYPAD != 0
    keypad_para_t param;
    param.text = (char *)lv_textarea_get_text(apn_ext->ta_name);
    param.password_mode = false;
    param.max_length = 10;
    param.min_length = 1;
    param.func = apn_get_keypad_text;
    keypad_create_btn_action(&param);
#endif
}

static void apn_prepare_destory(lv_obj_t * activity_obj)
{
    printf("%s\n", __FUNCTION__);
    lv_watch_png_cache_all_free();
}

static lv_obj_t * apn_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_APN;
        activity_ext.create = apn_create;
        activity_ext.prepare_destory = apn_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
    }

    lv_obj_t * obj;
    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    lv_apn_obj_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_apn_obj_ext_t));
    LV_ASSERT_MEM(ext);

    lv_obj_t * title_label = lv_label_create(obj, NULL);
    lv_obj_add_style(title_label, LV_CONT_PART_MAIN, &lv_watch_font20_gray);
    lv_label_set_align(title_label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(title_label, false);
    lv_label_set_text_id(title_label, WATCH_TEXT_ID_MODIFY_APN);
    lv_obj_align(title_label, obj, LV_ALIGN_IN_TOP_MID, 0, 10);

    //APN name
    lv_obj_t * cont_name = lv_cont_create(obj, NULL);
    lv_cont_set_layout(cont_name, LV_LAYOUT_ROW_MID);
    lv_cont_set_fit2(cont_name, LV_FIT_NONE, LV_FIT_TIGHT);
    lv_obj_set_width(cont_name, lv_obj_get_width(obj) - 10);
    lv_obj_add_style(cont_name, LV_CONT_PART_MAIN, &lv_watch_style_transp);
    lv_obj_align(cont_name, title_label, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);
    lv_watch_obj_add_element(cont_name);

    lv_obj_t * label_name = lv_label_create(cont_name, NULL);
    lv_obj_add_style(label_name, LV_CONT_PART_MAIN, &lv_watch_font20);
    lv_label_set_text_id(label_name, WATCH_TEXT_ID_NAME);

    ext->ta_name = lv_textarea_create(cont_name, NULL);
    lv_obj_set_width(ext->ta_name, 130);
    lv_textarea_set_max_length(ext->ta_name, 99);
    lv_textarea_set_one_line(ext->ta_name, true);
    lv_obj_set_style_local_text_font(ext->ta_name, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_FONT_SMALL);

    lv_mobile_network_obj_ext_t * network_ext = mobile_network_get_ext();
    if(strlen(network_ext->apn_name) > 0) {
        lv_textarea_set_text(ext->ta_name, network_ext->apn_name);
    } else {
        lv_textarea_set_text(ext->ta_name, "");
    }
    lv_watch_obj_add_element(ext->ta_name);

    lv_obj_t * btn_edit = lv_imgbtn_create(cont_name, NULL);
    lv_obj_set_style_local_text_font(btn_edit, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_SYMBOL_NORMAL);
    lv_imgbtn_set_src(btn_edit, LV_BTN_STATE_RELEASED, LV_SYMBOL_EDIT);
    lv_imgbtn_set_src(btn_edit, LV_BTN_STATE_PRESSED, LV_SYMBOL_EDIT);
    lv_obj_set_event_cb(btn_edit, apn_edit_btn_action);

    //IP type
    lv_obj_t * cont_ip_type = lv_cont_create(obj, cont_name);
    lv_obj_align(cont_ip_type, cont_name, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 10);
    lv_watch_obj_add_element(cont_ip_type);

    lv_obj_t * label_ip_type = lv_label_create(cont_ip_type, NULL);
    lv_obj_add_style(label_ip_type, LV_CONT_PART_MAIN, &lv_watch_font20);
    lv_label_set_text_id(label_ip_type, WATCH_TEXT_ID_TYPE);

    ext->ddlist_ip_type = lv_dropdown_create(cont_ip_type, NULL);
    lv_obj_set_style_local_text_font(ext->ddlist_ip_type, LV_DROPDOWN_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_FONT_SMALL);
    const char *options = "IPv4\nIPv6\nIPv4v6";
    lv_dropdown_set_options_static(ext->ddlist_ip_type, options);
    lv_obj_set_top(cont_ip_type, true);

    uint8_t index = 0;
    switch(network_ext->ip_type) {
        case CM_IPTYPE_IP:
            index = 0;
            break;

        case CM_IPTYPE_IPV6:
            index = 1;
            break;

        case CM_IPTYPE_IPV4V6:
            index = 2;
            break;

        default:
            break;
    }
    lv_dropdown_set_selected(ext->ddlist_ip_type, index);

    //btn
    lv_obj_t  * btn = lv_imgbtn_create(obj, NULL);
    lv_imgbtn_set_src(btn, LV_BTN_STATE_RELEASED, ICON_CONFIRM_BIG);
    lv_imgbtn_set_src(btn, LV_BTN_STATE_PRESSED, ICON_CONFIRM_BIG);
    lv_obj_set_event_cb(btn, apn_confirm_btn_action);
    lv_watch_obj_add_element(btn);
    lv_obj_align(btn, obj, LV_ALIGN_IN_BOTTOM_LEFT, 20, -20);

    btn = lv_imgbtn_create(obj, NULL);
    lv_imgbtn_set_src(btn, LV_BTN_STATE_RELEASED, ICON_CANCEL_BIG);
    lv_imgbtn_set_src(btn, LV_BTN_STATE_PRESSED, ICON_CANCEL_BIG);
    lv_obj_set_event_cb(btn, apn_cancel_btn_action);
    lv_watch_obj_add_element(btn);
    lv_obj_align(btn, obj, LV_ALIGN_IN_BOTTOM_RIGHT, -20, -20);

    return obj;
}
#if USE_LV_WATCH_CCFC != 0
static void call_forward_prepare_destory(lv_obj_t * activity_obj)
{
    printf("%s\n", __FUNCTION__);
    lv_watch_png_cache_all_free();
}

static lv_obj_t * call_forward_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_CALL_FORWARD;
        activity_ext.create = call_forward_create;
        activity_ext.prepare_destory = call_forward_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
    }

    lv_obj_t * obj;
    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    lv_call_forward_obj_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_call_forward_obj_ext_t));
    LV_ASSERT_MEM(ext);

    lv_obj_t * title_label = lv_label_create(obj, NULL);
    lv_obj_add_style(title_label, LV_CONT_PART_MAIN, &lv_watch_font20_gray);
    lv_label_set_align(title_label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(title_label, false);
    lv_label_set_text_id(title_label, WATCH_TEXT_ID_CALL_FORWARD);
    lv_obj_align(title_label, obj, LV_ALIGN_IN_TOP_MID, 0, 10);

    lv_obj_t * page = lv_page_create(obj, NULL);
    lv_obj_set_size(page, LV_HOR_RES, LV_VER_RES - lv_obj_get_height(title_label) - 26);
    lv_obj_align(page, title_label, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_obj_add_style(page, LV_PAGE_PART_BG, &lv_style_transp);
    lv_obj_add_style(page, LV_PAGE_PART_SCROLLABLE, &lv_style_transp);
    lv_obj_add_style(page, LV_PAGE_PART_EDGE_FLASH, &lv_watch_cont_opa1);
    lv_page_set_scrollbar_mode(page, LV_SCROLLBAR_MODE_OFF);
    lv_watch_obj_add_element(page);
    lv_watch_obj_add_element(lv_page_get_scrl(page));

    //no condition divert
    lv_obj_t * cont_no_condition = lv_cont_create(page, NULL);
    lv_obj_set_size(cont_no_condition, LV_HOR_RES, 40);
    lv_obj_add_style(cont_no_condition, LV_CONT_PART_MAIN, &lv_watch_style_transp);
    lv_obj_set_click(cont_no_condition, true);
    lv_obj_set_event_cb(cont_no_condition, mobile_network_no_condition_action);
    lv_watch_obj_add_element(cont_no_condition);
    lv_obj_align(cont_no_condition, title_label, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);

    lv_obj_t * no_contion_label = lv_label_create(cont_no_condition, NULL);
    lv_label_set_text_id(no_contion_label, WATCH_TEXT_ID_CALL_FORWARD_NO_CONDITION);
    lv_obj_add_style(no_contion_label, LV_CONT_PART_MAIN, &lv_watch_font20);
    lv_obj_t * no_condition_tip_label = lv_label_create(cont_no_condition, NULL);
    MMI_Modem_Call_Forward_Info_t *no_condition_info = watch_modem_call_forward_query_req(MMI_MODEM_CALL_FORWARD_NO_CONDITION);
    if(no_condition_info->CallForwardOn) {
        lv_label_set_text(no_condition_tip_label, no_condition_info->DestNum);
    } else {
        lv_label_set_text_id(no_condition_tip_label, WATCH_TEXT_ID_CALL_FORWARD_NOT_ENABLED);
    }
    lv_obj_add_style(no_condition_tip_label, LV_CONT_PART_MAIN, &lv_watch_font10);
    ext->no_condition_tip_label = no_condition_tip_label;

    lv_obj_t * no_condition_btn_right = lv_imgbtn_create(cont_no_condition, NULL);
    lv_obj_set_style_local_text_font(no_condition_btn_right, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_SYMBOL_NORMAL);
    lv_obj_set_click(no_condition_btn_right, false);

    //busy divert
    lv_obj_t * cont_busy = lv_cont_create(page, cont_no_condition);
    lv_obj_align(cont_busy, cont_no_condition, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_obj_set_click(cont_busy, true);
    lv_obj_set_event_cb(cont_busy, mobile_network_busy_action);
    lv_watch_obj_add_element(cont_busy);

    lv_obj_t * busy_label = lv_label_create(cont_busy, no_contion_label);
    lv_label_set_text_id(busy_label, WATCH_TEXT_ID_CALL_FORWARD_BUSY);
    lv_obj_t * busy_tip_label = lv_label_create(cont_busy, no_condition_tip_label);
    MMI_Modem_Call_Forward_Info_t *busy_info = watch_modem_call_forward_query_req(MMI_MODEM_CALL_FORWARD_BUSY);
    if(busy_info->CallForwardOn) {
        lv_label_set_text(busy_tip_label, busy_info->DestNum);
    } else {
        lv_label_set_text_id(busy_tip_label, WATCH_TEXT_ID_CALL_FORWARD_NOT_ENABLED);
    }
    ext->busy_tip_label = busy_tip_label;

    lv_obj_t * busy_btn_right = lv_imgbtn_create(cont_busy, NULL);
    lv_obj_set_style_local_text_font(busy_btn_right, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_SYMBOL_NORMAL);
    lv_obj_set_click(busy_btn_right, false);

    //not reach divert
    lv_obj_t * cont_not_reach = lv_cont_create(page, cont_busy);
    lv_obj_align(cont_not_reach, cont_busy, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_obj_set_click(cont_not_reach, true);
    lv_obj_set_event_cb(cont_not_reach, mobile_network_not_reach_action);
    lv_watch_obj_add_element(cont_not_reach);

    lv_obj_t * not_reach_label = lv_label_create(cont_not_reach, busy_label);
    lv_label_set_text_id(not_reach_label, WATCH_TEXT_ID_CALL_FORWARD_NOT_REACH);
    lv_obj_t * not_reach_tip_label = lv_label_create(cont_not_reach, busy_tip_label);
    MMI_Modem_Call_Forward_Info_t *not_reach_info = watch_modem_call_forward_query_req(MMI_MODEM_CALL_FORWARD_NOT_REACH);
    if(not_reach_info->CallForwardOn) {
        lv_label_set_text(not_reach_tip_label, not_reach_info->DestNum);
    } else {
        lv_label_set_text_id(not_reach_tip_label, WATCH_TEXT_ID_CALL_FORWARD_NOT_ENABLED);
    }
    ext->not_reach_tip_label = not_reach_tip_label;

    lv_obj_t * not_reach_btn_right = lv_imgbtn_create(cont_not_reach, NULL);
    lv_obj_set_style_local_text_font(not_reach_btn_right, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_SYMBOL_NORMAL);
    lv_obj_set_click(not_reach_btn_right, false);

    //no answer divert
    lv_obj_t * cont_no_answer = lv_cont_create(page, cont_not_reach);
    lv_obj_align(cont_no_answer, cont_not_reach, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_obj_set_click(cont_no_answer, true);
    lv_obj_set_event_cb(cont_no_answer, mobile_network_no_answer_action);
    lv_watch_obj_add_element(cont_no_answer);

    lv_obj_t * no_answer_label = lv_label_create(cont_no_answer, not_reach_label);
    lv_label_set_text_id(no_answer_label, WATCH_TEXT_ID_CALL_FORWARD_NO_ANSWER);
    lv_obj_t * no_answer_tip_label = lv_label_create(cont_no_answer, not_reach_tip_label);
    MMI_Modem_Call_Forward_Info_t *no_answer_info = watch_modem_call_forward_query_req(MMI_MODEM_CALL_FORWARD_NO_ANSWER);
    if(no_answer_info->CallForwardOn) {
        lv_label_set_text(no_answer_tip_label, no_answer_info->DestNum);
    } else {
        lv_label_set_text_id(no_answer_tip_label, WATCH_TEXT_ID_CALL_FORWARD_NOT_ENABLED);
    }
    ext->no_answer_tip_label = no_answer_tip_label;

    lv_obj_t * no_answer_btn_right = lv_imgbtn_create(cont_no_answer, NULL);
    lv_obj_set_style_local_text_font(no_answer_btn_right, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_SYMBOL_NORMAL);
    lv_obj_set_click(no_answer_btn_right, false);

    if(LV_BIDI_DIR_RTL == lv_obj_get_base_dir(page)) {
        lv_obj_align(no_contion_label, NULL, LV_ALIGN_IN_TOP_RIGHT, -10, 0);
        lv_obj_align(no_condition_tip_label, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -10, 0);
        lv_imgbtn_set_src(no_condition_btn_right, LV_BTN_STATE_RELEASED, LV_SYMBOL_LEFT);
        lv_imgbtn_set_src(no_condition_btn_right, LV_BTN_STATE_PRESSED, LV_SYMBOL_LEFT);
        lv_obj_align(no_condition_btn_right, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 10, 0);

        lv_obj_align(busy_label, NULL, LV_ALIGN_IN_TOP_RIGHT, -10, 0);
        lv_obj_align(busy_tip_label, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -10, 0);
        lv_imgbtn_set_src(busy_btn_right, LV_BTN_STATE_RELEASED, LV_SYMBOL_LEFT);
        lv_imgbtn_set_src(busy_btn_right, LV_BTN_STATE_PRESSED, LV_SYMBOL_LEFT);
        lv_obj_align(busy_btn_right, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 10, 0);

        lv_obj_align(not_reach_label, NULL, LV_ALIGN_IN_TOP_RIGHT, -10, 0);
        lv_obj_align(not_reach_tip_label, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -10, 0);
        lv_imgbtn_set_src(not_reach_btn_right, LV_BTN_STATE_RELEASED, LV_SYMBOL_LEFT);
        lv_imgbtn_set_src(not_reach_btn_right, LV_BTN_STATE_PRESSED, LV_SYMBOL_LEFT);
        lv_obj_align(not_reach_btn_right, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 10, 0);

        lv_obj_align(no_answer_label, NULL, LV_ALIGN_IN_TOP_RIGHT, -10, 0);
        lv_obj_align(no_answer_tip_label, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -10, 0);
        lv_imgbtn_set_src(no_answer_btn_right, LV_BTN_STATE_RELEASED, LV_SYMBOL_LEFT);
        lv_imgbtn_set_src(no_answer_btn_right, LV_BTN_STATE_PRESSED, LV_SYMBOL_LEFT);
        lv_obj_align(no_answer_btn_right, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 10, 0);
    } else {
        lv_obj_align(no_contion_label, NULL, LV_ALIGN_IN_TOP_LEFT, 10, 0);
        lv_obj_align(no_condition_tip_label, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 10, 0);
        lv_imgbtn_set_src(no_condition_btn_right, LV_BTN_STATE_RELEASED, LV_SYMBOL_RIGHT);
        lv_imgbtn_set_src(no_condition_btn_right, LV_BTN_STATE_PRESSED, LV_SYMBOL_RIGHT);
        lv_obj_align(no_condition_btn_right, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -10, 0);

        lv_obj_align(busy_label, NULL, LV_ALIGN_IN_TOP_LEFT, 10, 0);
        lv_obj_align(busy_tip_label, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 10, 0);
        lv_imgbtn_set_src(busy_btn_right, LV_BTN_STATE_RELEASED, LV_SYMBOL_RIGHT);
        lv_imgbtn_set_src(busy_btn_right, LV_BTN_STATE_PRESSED, LV_SYMBOL_RIGHT);
        lv_obj_align(busy_btn_right, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -10, 0);

        lv_obj_align(not_reach_label, NULL, LV_ALIGN_IN_TOP_LEFT, 10, 0);
        lv_obj_align(not_reach_tip_label, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 10, 0);
        lv_imgbtn_set_src(not_reach_btn_right, LV_BTN_STATE_RELEASED, LV_SYMBOL_RIGHT);
        lv_imgbtn_set_src(not_reach_btn_right, LV_BTN_STATE_PRESSED, LV_SYMBOL_RIGHT);
        lv_obj_align(not_reach_btn_right, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -10, 0);

        lv_obj_align(no_answer_label, NULL, LV_ALIGN_IN_TOP_LEFT, 10, 0);
        lv_obj_align(no_answer_tip_label, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 10, 0);
        lv_imgbtn_set_src(no_answer_btn_right, LV_BTN_STATE_RELEASED, LV_SYMBOL_RIGHT);
        lv_imgbtn_set_src(no_answer_btn_right, LV_BTN_STATE_PRESSED, LV_SYMBOL_RIGHT);
        lv_obj_align(no_answer_btn_right, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -10, 0);
    }

    return obj;
}

#if USE_LV_WATCH_KEYPAD != 0
static void divert_num_get_keypad_text(char *text)
{
    lv_call_forward_obj_ext_t * ext = call_forward_get_ext();
    if(NULL == ext) return;

    if(text) {
        switch (ext->divert_page)
        {
        case WATCH_CALL_FORWARD_NO_CONDITION:
            lv_textarea_set_text(ext->no_condition_number, text);
            break;
        case WATCH_CALL_FORWARD_BUSY:
            lv_textarea_set_text(ext->busy_number, text);
            break;
        case WATCH_CALL_FORWARD_NOT_REACH:
            lv_textarea_set_text(ext->not_reach_number, text);
            break;
        case WATCH_CALL_FORWARD_NO_ANSWER:
            lv_textarea_set_text(ext->no_answer_number, text);
            break;

        default:
            break;
        }
    }
    else {
        switch (ext->divert_page)
        {
        case WATCH_CALL_FORWARD_NO_CONDITION:
            lv_textarea_set_text(ext->no_condition_number, "");
            break;
        case WATCH_CALL_FORWARD_BUSY:
            lv_textarea_set_text(ext->busy_number, "");
            break;
        case WATCH_CALL_FORWARD_NOT_REACH:
            lv_textarea_set_text(ext->not_reach_number, "");
            break;
        case WATCH_CALL_FORWARD_NO_ANSWER:
            lv_textarea_set_text(ext->no_answer_number, "");
            break;

        default:
            break;
        }
    }
}
#endif

static void divert_num_edit_btn_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    lv_call_forward_obj_ext_t * ext = call_forward_get_ext();
    if(NULL == ext) {
        return;
    }

#if USE_LV_WATCH_KEYPAD != 0
    keypad_para_t param;
    switch (ext->divert_page)
    {
    case WATCH_CALL_FORWARD_NO_CONDITION:
        param.text = (char *)lv_textarea_get_text(ext->no_condition_number);
        break;
    case WATCH_CALL_FORWARD_BUSY:
        param.text = (char *)lv_textarea_get_text(ext->busy_number);
        break;
    case WATCH_CALL_FORWARD_NOT_REACH:
        param.text = (char *)lv_textarea_get_text(ext->not_reach_number);
        break;
    case WATCH_CALL_FORWARD_NO_ANSWER:
        param.text = (char *)lv_textarea_get_text(ext->no_answer_number);
        break;

    default:
        break;
    }
    param.password_mode = false;
    param.max_length = NV_CONTACTS_MAX_NUMBER_LEN - 1;
    param.min_length = 0;
    param.func = divert_num_get_keypad_text;
    keypad_create_btn_action(&param);
#endif
}

static void divert_confirm_btn_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    lv_call_forward_obj_ext_t * ext = call_forward_get_ext();
    if(NULL == ext) {
        return;
    }
    char * num = NULL;
    MMI_Modem_Call_Forward_Info_t * callforward_info = NULL;
    switch (ext->divert_page)
    {
    case WATCH_CALL_FORWARD_NO_CONDITION:
        num = (char *)lv_textarea_get_text(ext->no_condition_number);
        callforward_info = (MMI_Modem_Call_Forward_Info_t *)lv_mem_alloc(sizeof(MMI_Modem_Call_Forward_Info_t));
        memset(callforward_info,0,sizeof(MMI_Modem_Call_Forward_Info_t));
        callforward_info->CallForwardType = MMI_MODEM_CALL_FORWARD_NO_CONDITION;
        if(0 != strlen(num)) {
            lv_label_set_text(ext->no_condition_tip_label, num);
            callforward_info->CallForwardOn = 1;
            memcpy(callforward_info->DestNum,num,strlen(num));
        }
        else {
            lv_label_set_text_id(ext->no_condition_tip_label, WATCH_TEXT_ID_CALL_FORWARD_NOT_ENABLED);
            callforward_info->CallForwardOn = 0;
        }
        watch_modem_call_forward_set_req(callforward_info);
        break;
    case WATCH_CALL_FORWARD_BUSY:
        num = (char *)lv_textarea_get_text(ext->busy_number);
        callforward_info = (MMI_Modem_Call_Forward_Info_t *)lv_mem_alloc(sizeof(MMI_Modem_Call_Forward_Info_t));
        memset(callforward_info,0,sizeof(MMI_Modem_Call_Forward_Info_t));
        callforward_info->CallForwardType = MMI_MODEM_CALL_FORWARD_BUSY;
        if(0 != strlen(num)) {
            lv_label_set_text(ext->busy_tip_label, num);
            callforward_info->CallForwardOn = 1;
            memcpy(callforward_info->DestNum,num,strlen(num));
        }
        else {
            lv_label_set_text_id(ext->busy_tip_label, WATCH_TEXT_ID_CALL_FORWARD_NOT_ENABLED);
            callforward_info->CallForwardOn = 0;
        }
        watch_modem_call_forward_set_req(callforward_info);
        break;
    case WATCH_CALL_FORWARD_NOT_REACH:
        num = (char *)lv_textarea_get_text(ext->not_reach_number);
        callforward_info = (MMI_Modem_Call_Forward_Info_t *)lv_mem_alloc(sizeof(MMI_Modem_Call_Forward_Info_t));
        memset(callforward_info,0,sizeof(MMI_Modem_Call_Forward_Info_t));
        callforward_info->CallForwardType = MMI_MODEM_CALL_FORWARD_NOT_REACH;
        if(0 != strlen(num)) {
            lv_label_set_text(ext->not_reach_tip_label, num);
            callforward_info->CallForwardOn = 1;
            memcpy(callforward_info->DestNum,num,strlen(num));
        }
        else {
            lv_label_set_text_id(ext->not_reach_tip_label, WATCH_TEXT_ID_CALL_FORWARD_NOT_ENABLED);
            callforward_info->CallForwardOn = 0;
        }
        watch_modem_call_forward_set_req(callforward_info);
        break;
    case WATCH_CALL_FORWARD_NO_ANSWER:
        num = (char *)lv_textarea_get_text(ext->no_answer_number);
        callforward_info = (MMI_Modem_Call_Forward_Info_t *)lv_mem_alloc(sizeof(MMI_Modem_Call_Forward_Info_t));
        memset(callforward_info,0,sizeof(MMI_Modem_Call_Forward_Info_t));
        callforward_info->CallForwardType = MMI_MODEM_CALL_FORWARD_NO_ANSWER;
        if(0 != strlen(num)) {
            lv_label_set_text(ext->no_answer_tip_label, num);
            callforward_info->CallForwardOn = 1;
            callforward_info->NoAnswerDelay = 20;
            memcpy(callforward_info->DestNum,num,strlen(num));
        }
        else {
            lv_label_set_text_id(ext->no_answer_tip_label, WATCH_TEXT_ID_CALL_FORWARD_NOT_ENABLED);
            callforward_info->CallForwardOn = 0;
        }
        watch_modem_call_forward_set_req(callforward_info);
        break;

    default:
        break;
    }
    if(0 == strlen(num)) {
        printf("%s: strlen(num) == 0\n", __FUNCTION__);
        //return;
    }

    printf("%s: %s\n", __FUNCTION__, num);

    lv_watch_obj_cleanup(btn);
}

static void divert_cancel_btn_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    lv_watch_obj_cleanup(btn);
}

static void call_forward_common_prepare_destory(lv_obj_t * activity_obj)
{
    printf("%s\n", __FUNCTION__);
    lv_call_forward_obj_ext_t * ext = call_forward_get_ext();
    LV_ASSERT_MEM(ext);
    ext->divert_page = WATCH_CALL_FORWARD_NULL;
    lv_watch_png_cache_all_free();
}

static lv_obj_t * call_forward_common_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_DIVERT_COMMON;
        activity_ext.create = call_forward_common_create;
        activity_ext.prepare_destory = call_forward_common_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
    }

    lv_obj_t * obj;
    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    lv_call_forward_obj_ext_t * ext = call_forward_get_ext();
    LV_ASSERT_MEM(ext);

    lv_obj_t * title_label = lv_label_create(obj, NULL);
    lv_obj_add_style(title_label, LV_CONT_PART_MAIN, &lv_watch_font20_gray);
    lv_label_set_align(title_label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(title_label, false);
    switch (ext->divert_page)
    {
    case WATCH_CALL_FORWARD_NO_CONDITION:
        lv_label_set_text_id(title_label, WATCH_TEXT_ID_CALL_FORWARD_NO_CONDITION);
        break;
    case WATCH_CALL_FORWARD_BUSY:
        lv_label_set_text_id(title_label, WATCH_TEXT_ID_CALL_FORWARD_BUSY);
        break;
    case WATCH_CALL_FORWARD_NOT_REACH:
        lv_label_set_text_id(title_label, WATCH_TEXT_ID_CALL_FORWARD_NOT_REACH);
        break;
    case WATCH_CALL_FORWARD_NO_ANSWER:
        lv_label_set_text_id(title_label, WATCH_TEXT_ID_CALL_FORWARD_NO_ANSWER);
        break;

    default:
        break;
    }
    lv_obj_align(title_label, obj, LV_ALIGN_IN_TOP_MID, 0, 10);

    //divert number
    lv_obj_t * cont_number = lv_cont_create(obj, NULL);
    lv_cont_set_layout(cont_number, LV_LAYOUT_ROW_MID);
    lv_cont_set_fit2(cont_number, LV_FIT_NONE, LV_FIT_TIGHT);
    lv_obj_set_width(cont_number, lv_obj_get_width(obj) - 4);
    lv_obj_add_style(cont_number, LV_CONT_PART_MAIN, &lv_watch_style_transp);
    lv_obj_align(cont_number, title_label, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);
    lv_watch_obj_add_element(cont_number);

    lv_obj_t * label_number = lv_label_create(cont_number, NULL);
    lv_obj_add_style(label_number, LV_CONT_PART_MAIN, &lv_watch_font20);
    lv_label_set_text_id(label_number, WATCH_TEXT_ID_NUMBER);
    lv_obj_t * ta_temp = lv_textarea_create(cont_number, NULL);

    switch (ext->divert_page)
    {
    case WATCH_CALL_FORWARD_NO_CONDITION:
        ext->no_condition_number = ta_temp;
        break;
    case WATCH_CALL_FORWARD_BUSY:
        ext->busy_number = ta_temp;
        break;
    case WATCH_CALL_FORWARD_NOT_REACH:
        ext->not_reach_number = ta_temp;
        break;
    case WATCH_CALL_FORWARD_NO_ANSWER:
        ext->no_answer_number = ta_temp;
        break;

    default:
        break;
    }
    lv_obj_set_width(ta_temp, 150);
    lv_textarea_set_max_length(ta_temp, 99);
    lv_textarea_set_one_line(ta_temp, true);
    lv_obj_set_style_local_text_font(ta_temp, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_FONT_SMALL);

    char * label_num = NULL;
    switch (ext->divert_page)
    {
    case WATCH_CALL_FORWARD_NO_CONDITION:
        ext->no_condition_number = ta_temp;
        label_num = lv_label_get_text(ext->no_condition_tip_label);
        break;
    case WATCH_CALL_FORWARD_BUSY:
        ext->busy_number = ta_temp;
        label_num = lv_label_get_text(ext->busy_tip_label);
        break;
    case WATCH_CALL_FORWARD_NOT_REACH:
        ext->not_reach_number = ta_temp;
        label_num = lv_label_get_text(ext->not_reach_tip_label);
        break;
    case WATCH_CALL_FORWARD_NO_ANSWER:
        ext->no_answer_number = ta_temp;
        label_num = lv_label_get_text(ext->no_answer_tip_label);
        break;

    default:
        break;
    }
    if(0 != strcmp((const char *)lv_lang_get_text(WATCH_TEXT_ID_CALL_FORWARD_NOT_ENABLED),label_num)) {
        lv_textarea_set_text(ta_temp, label_num);
    }
    else {
        lv_textarea_set_text(ta_temp, "");
    }
    lv_textarea_set_accepted_chars(ta_temp, g_accept_input_num);
    lv_watch_obj_add_element(ta_temp);

    lv_obj_t * btn_edit = lv_imgbtn_create(cont_number, NULL);
    lv_obj_set_style_local_text_font(btn_edit, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_SYMBOL_NORMAL);
    lv_imgbtn_set_src(btn_edit, LV_BTN_STATE_RELEASED, LV_SYMBOL_EDIT);
    lv_imgbtn_set_src(btn_edit, LV_BTN_STATE_PRESSED, LV_SYMBOL_EDIT);
    lv_obj_set_event_cb(btn_edit, divert_num_edit_btn_action);

    //btn
    lv_obj_t  * btn = lv_imgbtn_create(obj, NULL);
    lv_imgbtn_set_src(btn, LV_BTN_STATE_RELEASED, ICON_CONFIRM_BIG);
    lv_imgbtn_set_src(btn, LV_BTN_STATE_PRESSED, ICON_CONFIRM_BIG);
    lv_obj_set_event_cb(btn, divert_confirm_btn_action);
    lv_watch_obj_add_element(btn);
    lv_obj_align(btn, obj, LV_ALIGN_IN_BOTTOM_LEFT, 20, -20);

    btn = lv_imgbtn_create(obj, NULL);
    lv_imgbtn_set_src(btn, LV_BTN_STATE_RELEASED, ICON_CANCEL_BIG);
    lv_imgbtn_set_src(btn, LV_BTN_STATE_PRESSED, ICON_CANCEL_BIG);
    lv_obj_set_event_cb(btn, divert_cancel_btn_action);
    lv_watch_obj_add_element(btn);
    lv_obj_align(btn, obj, LV_ALIGN_IN_BOTTOM_RIGHT, -20, -20);

    return obj;
}
#endif
#endif /*USE_LV_WATCH_MOBILE_NETWORK*/
